package com.kancy.springcloudconfig.constants;

import com.kancy.springcloudconfig.enums.AlgorithmType;
import com.kancy.springcloudconfig.util.CommonUtil;

import java.io.File;
import java.io.FileInputStream;
import java.util.*;

/**
 * @author huangchengkang
 */
public class Constants {


    public static String DEFAULT_TITLE= "Spring Cloud Config 解密工具 v1.4";

    public static String DEFAULT_MENU_TITLE= "隐藏菜单";

    public static final String FOLLOW_ME_URL = "https://gitee.com/kancy666/spring-cloud-config-encryptor";

    public static String LOOK_AND_FEEL = "random";

    public static Set<AlgorithmType> supportAlgorithm = new HashSet<>();

    public static String DEFAULT_AES_PASSWORD = "UalUV^u@F$2ZStxE";

    public static String DEFAULT_AES_SALT = "lr60I@gpel#IIkSG";

    public static String DEFAULT_RSA_PUBLIC_KEY = "";
    public static String DEFAULT_RSA_PRIVATE_KEY = "";

    public static String DEFAULT_CIPHER_PASSWORD = "dmNyZWRpdHZtcA";
    public static String DEFAULT_CIPHER_SALT = "deadbeef";

    public static String DEFAULT_JASYPT_PASSWORD = "dmNyZWRpdHZtcA";

    public static String DEFAULT_KMS_ID = "vccx";
    public static String DEFAULT_KMS_KEY = "qWuvTYjgVOfoJeBriLHy2Ns9lGwh8zER";

    private static final String CONFIG_FILE_PATH = System.getProperty("user.home") + "\\scc.ini";

    static {
        initConfig();
    }

    private static void initConfig() {
        try {
            System.out.println("加载："+CONFIG_FILE_PATH);
            File file = new File(CONFIG_FILE_PATH);
            if (!file.exists()){
                file.createNewFile();
            }

            Properties properties = new Properties();
            properties.load(new FileInputStream(file));

            String key = "kms.id";
            if (properties.containsKey(key)){
                DEFAULT_KMS_ID = properties.getProperty(key, DEFAULT_KMS_ID);
            }

            key = "kms.key";
            if (properties.containsKey(key)){
                DEFAULT_KMS_KEY = properties.getProperty(key, DEFAULT_KMS_KEY);
            }

            key = "jasypt.password";
            if (properties.containsKey(key)){
                DEFAULT_JASYPT_PASSWORD = properties.getProperty(key, DEFAULT_JASYPT_PASSWORD);
            }

            key = "cipher.password";
            if (properties.containsKey(key)){
                DEFAULT_CIPHER_PASSWORD = properties.getProperty(key, DEFAULT_CIPHER_PASSWORD);
            }

            key = "cipher.salt";
            if (properties.containsKey(key)){
                DEFAULT_CIPHER_SALT = properties.getProperty(key, DEFAULT_CIPHER_SALT);
            }

            key = "aes.password";
            if (properties.containsKey(key)){
                DEFAULT_AES_PASSWORD = properties.getProperty(key, DEFAULT_AES_PASSWORD);
            }

            key = "aes.iv";
            if (properties.containsKey(key)){
                DEFAULT_AES_SALT = properties.getProperty(key, DEFAULT_AES_SALT);
            }

            key = "rsa.public.key";
            if (properties.containsKey(key)){
                DEFAULT_RSA_PUBLIC_KEY = properties.getProperty(key, DEFAULT_RSA_PUBLIC_KEY);
            }

            key = "rsa.private.key";
            if (properties.containsKey(key)){
                DEFAULT_RSA_PRIVATE_KEY = properties.getProperty(key, DEFAULT_RSA_PRIVATE_KEY);
            }

            key = "title";
            if (properties.containsKey(key)){
                DEFAULT_TITLE = properties.getProperty(key, DEFAULT_TITLE);
            }

            key = "ui.theme";
            if (properties.containsKey(key)){
                LOOK_AND_FEEL = properties.getProperty(key, LOOK_AND_FEEL);
            }

            key = "algorithm";
            if (properties.containsKey(key)){
                String property = properties.getProperty(key);
                if (CommonUtil.isNotEmpty(property)){
                    String[] strings = property.split(",");
                    for (String enumName : strings) {
                        try {
                            AlgorithmType algorithmType = AlgorithmType.valueOf(enumName);
                            supportAlgorithm.add(algorithmType);
                        } catch (Exception e) {
                        }
                    }
                }
            }

        } catch (Exception e) {
            System.out.println("加载配置文件失败！");
        }
    }

}
